package com.autumn.scheduler.job;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.quartz.DateBuilder.IntervalUnit;

import com.autumn.scheduler.api.constant.ApiConstants.JobIntervalUnit;

/**
 * 常量
 * 
 * @author 老码农
 *
 *         2018-03-19 21:01:16
 */
public class JobConstants {

	/**
	 * 调度器名称
	 */
	public final static String AUTUMN_QUARTZ_SCHEDULER = "AutumnQuartzScheduler";

	/**
	 * Job 数据键 描述：
	 * 
	 * @author 杨昌国 2018-03-19 23:36:29
	 */
	public static class JobDataKey {

		/**
		 * 任务友好名称
		 */
		public final static String JOB_FRIENDLY_NAME = "JOB_FRIENDLY_NAME";

		/**
		 * 运行次数
		 */
		public final static String RUN_NUMBERS = "RUN_NUMBERS";

		/**
		 * 执行指定次数后不再执行,<0表示不限
		 */
		public final static String EXECUTE_NUMBERS = "EXECUTE_NUMBERS";

		/**
		 * 执行后操作
		 */
		public final static String EXECUTE_OPERATION = "EXECUTE_OPERATION";

		/**
		 * 作业队列名称
		 */
		public final static String JOB_QUEUE_NAME = "JOB_QUEUE_NAME";

		/**
		 * 执行参数
		 */
		public final static String EXECUTE_PARAMETER_MAP = "EXECUTE_PARAMETER_MAP";
	}

	/**
	 * 时间单位地图
	 * 
	 * @author 老码农
	 *
	 *         2018-03-23 14:46:53
	 */
	public static class JobIntervalUnitMap {

		/**
		 * Job 时间单位信息
		 * 
		 * @author 老码农
		 *
		 *         2018-03-23 14:57:49
		 */
		public static class JobIntervalUnitInfo {

			private final String name;
			private final IntervalUnit unit;
			private final String Explain;

			public JobIntervalUnitInfo(String name, IntervalUnit unit, String explain) {
				super();
				this.name = name;
				this.unit = unit;
				Explain = explain;
			}

			public String getName() {
				return name;
			}

			public IntervalUnit getUnit() {
				return unit;
			}

			public String getExplain() {
				return Explain;
			}

		}

		private final static Map<String, JobIntervalUnitInfo> INTERVAL_UNIT_MAP = new HashMap<>();
		static {
			JobIntervalUnitInfo unitInfo;
			unitInfo = new JobIntervalUnitInfo(JobIntervalUnit.MILLISECOND, IntervalUnit.MILLISECOND, "毫秒");
			INTERVAL_UNIT_MAP.put(unitInfo.getName(), unitInfo);
			unitInfo = new JobIntervalUnitInfo(JobIntervalUnit.SECOND, IntervalUnit.SECOND, "秒");
			INTERVAL_UNIT_MAP.put(unitInfo.getName(), unitInfo);
			unitInfo = new JobIntervalUnitInfo(JobIntervalUnit.MINUTE, IntervalUnit.MINUTE, "分钟");
			INTERVAL_UNIT_MAP.put(unitInfo.getName(), unitInfo);
			unitInfo = new JobIntervalUnitInfo(JobIntervalUnit.HOUR, IntervalUnit.HOUR, "小时");
			INTERVAL_UNIT_MAP.put(unitInfo.getName(), unitInfo);
			unitInfo = new JobIntervalUnitInfo(JobIntervalUnit.DAY, IntervalUnit.DAY, "天");
			INTERVAL_UNIT_MAP.put(unitInfo.getName(), unitInfo);
			unitInfo = new JobIntervalUnitInfo(JobIntervalUnit.WEEK, IntervalUnit.WEEK, "星期");
			INTERVAL_UNIT_MAP.put(unitInfo.getName(), unitInfo);
			unitInfo = new JobIntervalUnitInfo(JobIntervalUnit.MONTH, IntervalUnit.MONTH, "月");
			INTERVAL_UNIT_MAP.put(unitInfo.getName(), unitInfo);
			unitInfo = new JobIntervalUnitInfo(JobIntervalUnit.YEAR, IntervalUnit.YEAR, "年");
			INTERVAL_UNIT_MAP.put(unitInfo.getName(), unitInfo);
		}

		/**
		 * 获取时间单位信息
		 * 
		 * @param name
		 *            名称
		 * @return
		 */
		public static JobIntervalUnitInfo getIntervalUnit(String name) {
			if (name != null) {
				name = name.toUpperCase();
			}
			JobIntervalUnitInfo msg = INTERVAL_UNIT_MAP.get(name);
			if (msg == null) {
				msg = null;
			}
			return msg;
		}

		/**
		 * 
		 * @return
		 *
		 */
		public static Collection<JobIntervalUnitInfo> timeIntervalUnitList() {
			return INTERVAL_UNIT_MAP.values();
		}
	}
}
